/**
 * 
 */
package edu.umd.clip.lm.model.training;

import java.util.*;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class NbestDataSplits<T> {
	private TreeSet<T> splits;
	private final int size;
	
	public NbestDataSplits(int size) {
		this.size = size;
		this.splits = new TreeSet<T>();
	}
	
	public synchronized <S extends T> T add(S split) {
		splits.add(split);
		if (splits.size() > size) {
			return splits.pollLast();
		}
		return null;
	}
	
	public synchronized T[] toArray(T[] sample) {
		return splits.toArray(sample);
	}

	public SortedSet<T> getSplits() {
		return splits;
	}
	
	public synchronized int size() {
		return splits.size();
	}
	
	public synchronized void clear() {
		splits.clear();
	}
}
